//
//  WaitForConfirmViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/25.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "WaitForConfirmViewController.h"
#import "InterviewManageModel.h"
#import "DeliveredResumeTableViewCell.h"
#import "DeliveredJobListTableViewCell.h"
#import "JobDetailViewController.h"
#import "PreviewResumeViewController.h"
#import <PGDatePickManager.h>
#import "ZYInputAlertView.h"
#import <Masonry.h>

@interface WaitForConfirmViewController ()<UITableViewDelegate, UITableViewDataSource, DeliveredResumeCellDelegate2, DeliveredJobListCellDelegate2,PGDatePickerDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *jobArr;
@property (strong, nonatomic) NSMutableArray *resumeArr;

@end

@implementation WaitForConfirmViewController

- (void)viewWillAppear:(BOOL)animated
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		[self getJobInfo];
	}
	else if(role == 5 || role == 6){
		[self getResumeInfo];
	}
}

- (void)viewDidLoad {
	[super viewDidLoad];
	
	// Do any additional setup after loading the view.
	[self createUI];
	[self refreshData];
}

- (void)createUI {
	
	self.view.backgroundColor = UISeparatedColorRGB;
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-120) style:UITableViewStylePlain];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	[_tableView registerNib:[UINib nibWithNibName:@"DeliveredResumeTableViewCell" bundle:nil] forCellReuseIdentifier:@"resumeCell"];
	[_tableView registerNib:[UINib nibWithNibName:@"DeliveredJobListTableViewCell" bundle:nil] forCellReuseIdentifier:@"jobListCell"];
	_tableView.backgroundColor = UISeparatedColorRGB;
	_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.view addSubview:_tableView];
	
}

- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
		if (role == 2 || role == 4) {
			[weakSelf getJobInfo];
		}
		else if(role == 5 || role == 6){
			[weakSelf getResumeInfo];
		}
	}];
}

//TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		DeliveredJobListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jobListCell" forIndexPath:indexPath];
		cell.type = self.type+1;
		cell.model = self.jobArr[indexPath.row];
		cell.delegate2 = self;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		if (self.type == 0) {
			[cell.remark mas_makeConstraints:^(MASConstraintMaker *make) {
				make.bottom.equalTo(cell.contentView).mas_offset(-15);//下
			}];
		}
		return cell;
		
	}
	else if(role == 5 || role == 6){
		DeliveredResumeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"resumeCell" forIndexPath:indexPath];
		cell.type = self.type+1;
		cell.model = self.resumeArr[indexPath.row];
		cell.delegate2 = self;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		if (self.type == 0) {
			[cell.remark mas_makeConstraints:^(MASConstraintMaker *make) {
				make.bottom.equalTo(cell.contentView).mas_offset(-15);//下
			}];
		}
		return cell;
	}
	
	else {
		return nil;
	}
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	/*
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		JobDetailViewController *vc = [[JobDetailViewController alloc] init];
		vc.position_id = self.jobArr[indexPath.row][@"position_id"];
		vc.isFromCompany = YES;	//一开始名字没设计好，应该叫shouldShowBtn之类的
		[self.navigationController pushViewController:vc animated:YES];
	}
	else if(role == 5 || role == 6){
		PreviewResumeViewController *vc = [[PreviewResumeViewController alloc] init];
		vc.resumeId = self.resumeArr[indexPath.row][@"resume_id"];
		vc.isFromCompany = NO;	//一开始名字没设计好，应该叫shouldShowBtn之类的
		[self.navigationController pushViewController:vc animated:YES];
	}
	 */
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		return self.jobArr.count;
	}
	else if(role == 5 || role == 6){
		return self.resumeArr.count;
	}
	else{
		return 0;
	}
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		if (self.type == 1 || self.type == 2) {
			return 180;
		}
		else {
			return 215;
		}
	}
	else {
		if (self.type == 1 || self.type == 2) {
			return 130;
		}
		else {
			return 180;
		}
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	
	if (role == 2 || role == 4) {
		if (self.type == 1 || self.type == 2) {
			return 170;
		}
		else {
			tableView.rowHeight = UITableViewAutomaticDimension;
			return tableView.rowHeight;
		}
	}
	else {
		if (self.type == 1 || self.type == 2) {
			return 130;
		}
		else {
			tableView.rowHeight = UITableViewAutomaticDimension;
			return tableView.rowHeight;
		}
	}
}

- (NSMutableArray *)jobArr
{
	if (!_jobArr) {
		_jobArr = [[NSMutableArray alloc] init];
	}
	return _jobArr;
}

- (NSMutableArray *)resumeArr
{
	if (!_resumeArr) {
		_resumeArr = [[NSMutableArray alloc] init];
	}
	return _resumeArr;
}

- (void)getJobInfo
{
	NSDictionary *param;
	if (self.type == 1) {
		param = @{@"status":@"1"};
	}
	else if(self.type == 2)
	{
		param = @{@"status":@"2"};
	}
	else {
		param = @{@"status":@"3"};
	}
	[[NetworkTool sharedTool] requestWithName:@"获取个人面试管理信息(待确认)" URLString:APIPersonInterviewManage parameters:param method:@"POST" callBack:^(id responseObject) {
		
		[self.jobArr removeAllObjects];
		NSArray *arr = responseObject[@"data"];
		for (NSDictionary *dict in arr){
			InterviewManageModel *model = [[InterviewManageModel alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[self.jobArr addObject:model];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[MBProgressHUD hideHUDForView:self.view animated:YES];
			[self.tableView.mj_header endRefreshing];
			[self.tableView reloadData];
		});
	}];
}

- (void)getResumeInfo
{
	NSDictionary *param;
	if (self.type == 1) {
		param = @{@"status":@"1"};
	}
	else if(self.type == 2)
	{
		param = @{@"status":@"2"};
	}
	else {
		param = @{@"status":@"3"};
	}
	[[NetworkTool sharedTool] requestWithName:@"获取企业面试管理信息(待确认)" URLString:APICompanyInterviewManage parameters:param method:@"POST" callBack:^(id responseObject) {
		
		[self.resumeArr removeAllObjects];
		NSArray *arr = responseObject[@"data"];
		for (NSDictionary *dict in arr){
			InterviewManageModel *model = [[InterviewManageModel alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[self.resumeArr addObject:model];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[MBProgressHUD hideHUDForView:self.view animated:YES];
			[self.tableView.mj_header endRefreshing];
			[self.tableView reloadData];
		});
	}];
}

- (void)chooseDateForInterviewId:(NSString *)interviewId
{
	PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
	PGDatePicker *datePicker = datePickManager.datePicker;
	datePicker.datePickerMode = PGDatePickerModeDateHourMinute;
	datePicker.delegate = self;
	[self presentViewController:datePickManager animated:false completion:nil];
	datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
		NSLog(@"dateComponents = %@", dateComponents);
		NSCalendar *calender = [NSCalendar currentCalendar];
		NSDate *date = [calender dateFromComponents:dateComponents];
		NSTimeInterval interval = [date timeIntervalSince1970];
		NSLog(@"时间戳%d",(int)interval);
		NSDictionary *param = @{@"interview_id":interviewId,
								@"time":[NSString stringWithFormat:@"%d", (int)interval]
								};
		[[NetworkTool sharedTool] requestWithName:@"企业选择时间(面试管理)" URLString:APICompanySetTime parameters:param method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	};
}

- (void)agreeDate:(NSString *)interviewId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定同意该时间？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"同意面试日期" URLString:APIPersonConfirmDate parameters:@{@"interview_id":interviewId} method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)dicussDate:(NSString *)interviewId
{
	__weak typeof(self) weakSelf = self;
	ZYInputAlertView *alertView = [ZYInputAlertView alertView];
	alertView.placeholder = @"输入备注信息以便企业选择时间（例:我9月16，17号下午两点有空）···";
	[alertView confirmBtnClickBlock:^(NSString *inputString) {
		NSDictionary *param = @{@"interview_id":interviewId,
								@"remark":inputString
								};
		[[NetworkTool sharedTool] requestWithName:@"个人备注信息协调时间" URLString:APIPersonSetRemark parameters:param method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[weakSelf getJobInfo];
			}
			else if(role == 5 || role == 6){
				[weakSelf getResumeInfo];
			}
		}];
	}];
	[alertView show];
}

- (void)employ:(NSString *)interviewId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定发放offer？若后续面试者接受offer将扣除相应面试服务费" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"确定录用" URLString:APIEmploy parameters:@{@"interview_id":interviewId} method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)notEmploy:(NSString *)interviewId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定拒绝该面试者？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"确定拒绝录用" URLString:APINotEmploy parameters:@{@"interview_id":interviewId} method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

-(void)agreeEmploy:(BOOL)agree interviewId:(NSString *)interviewId
{
	if (agree) {
		UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定接受该offer？同意后将扣除相应面试服务费" message:nil preferredStyle:UIAlertControllerStyleAlert];
		[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		}]];
		[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
			[[NetworkTool sharedTool] requestWithName:@"确定接受offer" URLString:APIPersonAgreeEmployOrNot parameters:@{@"interview_id":interviewId, @"type":@"1"} method:@"POST" callBack:^(id responseObject) {
				NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
				if (role == 2 || role == 4) {
					[self getJobInfo];
				}
				else if(role == 5 || role == 6){
					[self getResumeInfo];
				}
			}];
		}]];
		[self presentViewController:alert animated:YES completion:nil];
	}
	else
	{
		UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定拒绝该offer？" message:nil preferredStyle:UIAlertControllerStyleAlert];
		[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		}]];
		[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
			[[NetworkTool sharedTool] requestWithName:@"确定拒绝offer" URLString:APIPersonAgreeEmployOrNot parameters:@{@"interview_id":interviewId, @"type":@"-1"} method:@"POST" callBack:^(id responseObject) {
				NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
				if (role == 2 || role == 4) {
					[self getJobInfo];
				}
				else if(role == 5 || role == 6){
					[self getResumeInfo];
				}
			}];
		}]];
		[self presentViewController:alert animated:YES completion:nil];
	}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
